//
//  ITSGetImageForCellOperation.m
//  AsiaScene
//
//  Created by Rainbow on 3/18/11.
//  Copyright 2011 iTotemStudio. All rights reserved.
//

#import "ITSGetImageForCellOperation.h"
#import "UtilHelper.h"


@implementation ITSGetImageForCellOperation
@synthesize index,_tableCell,_imageUrl,_imageName;

-(id)initWithTableCell:(ITSTableViewCell *)tableCell imageUrl:(NSString *)url nameToSave:(NSString *)name
{
	if ((self = [super init])) {
		self._imageUrl = url;
        self._imageUrl = [self._imageUrl stringByReplacingOccurrencesOfString:@" " withString:@""];
		self._imageName = name;
		self._tableCell = tableCell;
	}
	return self;
}


-(id)initWithTableCell:(ITSTableViewCell *)tableCell imageUrl:(NSString *)url
{
    if ((self = [super init])) {
		self._imageUrl = url;
        self._imageUrl = [self._imageUrl stringByReplacingOccurrencesOfString:@" " withString:@""];
		self._tableCell = tableCell;
		self._imageName = nil;
	}
	return self;
}

-(void)main
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    if (self._imageUrl == nil||[@"" isEqualToString:self._imageUrl]) {
        [self._tableCell performSelectorOnMainThread:@selector(imageDidLoad:) withObject:nil waitUntilDone:YES];
    }
    else
    {
        UIImage *image = [UtilHelper getImageFromUrl:self._imageUrl];
        if (image != nil && _imageName) {
            [UtilHelper storeImageInDocument:image ImageName:self._imageName];
        }
        
        if (image != nil) {
            
            NSArray *array = [NSArray arrayWithObjects:image,[NSNumber numberWithInt:self.index], nil];
            [self._tableCell performSelectorOnMainThread:@selector(imageDidLoad:) withObject:array waitUntilDone:YES];
        }
        else
        {
            [self._tableCell performSelectorOnMainThread:@selector(imageDidLoad:) withObject:nil waitUntilDone:YES];
        }
        
    }
	[pool release];
}

-(void)dealloc
{
    _tableCell = nil;
    _imageUrl = nil;
    _imageName = nil;
    [super dealloc];
}

@end
